using UnityEngine;
using System.Collections;

public class Ghost : DrawableGameObject 
{
	public GameObject player;
	public float speed;
	
	bool switcher;
	
	Vector3 startScale;
	
	public Texture2D[] textures;
	
	public override void Start () 
	{
		base.Start();
		base.SetAnimations(textures, null, null, null);
		startScale = transform.localScale;
	}
	
	void Update () 
	{
		RunAnimations();
		
		if(switcher)
		{
			if(transform.position.x > player.transform.position.x)
			{
				transform.Translate(-speed,0,0);
				transform.localScale = new Vector3(startScale.x, startScale.y, startScale.z);
			}
			
			if(transform.position.x < player.transform.position.x)
			{
				transform.Translate(speed,0,0);
				transform.localScale = new Vector3(-startScale.x, startScale.y, startScale.z);
			}
			
			if(transform.position.y > player.transform.position.y)
			{
				transform.Translate(0,-speed,0);
			}
			
			if(transform.position.y < player.transform.position.y)
			{
				transform.Translate(0,speed,0);
			}
		}
	}
	
	public void OnTriggerEnter(Collider other)
	{
		if(other.gameObject.tag == "Player")
		{
			switcher = true;
		}
	}
	
	public void OnTriggerExit(Collider other)
	{
		if(other.gameObject.tag == "Player")
		{
			switcher = false;
		}
	}
	
	public void RunAnimations()
	{
		base.gameAnimation.CalculateFps(5);
		base.RunAnimation("Stand", "Loop");
	}
}
